package com.wm.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.commonutils.R;
import com.wm.eduService.entity.EduTeacher;
import com.wm.eduService.entity.vo.TeacherQuery;
import com.wm.eduService.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author sshdg
 * @since 2020-09-26
 */
//@CrossOrigin
@RestController
@RequestMapping("/eduService/teacher")
@Api(tags = "讲师管理")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;


    @GetMapping("/findAll")
    @ApiOperation("所有讲师列表")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);

    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "逻辑删除的讲师id", name = "id", required = true, paramType = "path"),
    })
    @DeleteMapping("/{id}")
    @ApiOperation("逻辑删除讲师")
    public R removeTeacher(@PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if (flag){
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "第几页", name = "current", required = true, paramType = "path"),
            @ApiImplicitParam(value = "页面大小", name = "limit", required = true, paramType = "path")
    })
    @ApiOperation("分页查询讲师(无查询条件)")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit){
        Page<EduTeacher> page = new Page<>(current, limit);
        eduTeacherService.page(page);
        // 总记录数
        long total = page.getTotal();
        // 分页查询的数据
        List<EduTeacher> records = page.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("分页查询讲师(带查询条件)")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody TeacherQuery teacherQuery){

        Page<EduTeacher> page = new Page<>(current, limit);

        LambdaQueryWrapper<EduTeacher> lambdaWrapper = new LambdaQueryWrapper<>();
        lambdaWrapper.like(StringUtils.isNotBlank(teacherQuery.getName()), EduTeacher::getName, teacherQuery.getName());
        lambdaWrapper.eq(Objects.nonNull(teacherQuery.getLevel()), EduTeacher::getLevel, teacherQuery.getLevel());
        lambdaWrapper.ge(StringUtils.isNotBlank(teacherQuery.getBegin()), EduTeacher::getGmtCreate, teacherQuery.getBegin());
        lambdaWrapper.le(StringUtils.isNotBlank(teacherQuery.getEnd()), EduTeacher::getGmtCreate, teacherQuery.getEnd());
        lambdaWrapper.orderByDesc(EduTeacher::getGmtCreate);

        eduTeacherService.page(page, lambdaWrapper);
        // 总记录数
        long total = page.getTotal();
        // 分页查询的数据
        List<EduTeacher> records = page.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("添加讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher teacher){
        boolean save = eduTeacherService.save(teacher);
        if (save){
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("/getTeacher/{id}")
    @ApiOperation("根据讲师id进行查询")
    public R getTeacher(@PathVariable("id") String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    @ApiOperation("修改讲师功能")
    @PutMapping("/updateTeacher/{id}")
    public R updateTeacher(@PathVariable String id,@RequestBody EduTeacher teacher){

        teacher.setId(id);
        boolean updateResult = eduTeacherService.updateById(teacher);

        if(updateResult){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

